package com.nari.ies.ne.nw.server.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
	public static SimpleDateFormat SDF_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static SimpleDateFormat SDF_DAY_HH_MM_SS = new SimpleDateFormat("yyyyMMddHHmmss");

	public static SimpleDateFormat SDF_MM = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	public static SimpleDateFormat SDF_DAY = new SimpleDateFormat("yyyy-MM-dd");

	public static SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMdd");

	public static SimpleDateFormat SDF_MI = new SimpleDateFormat("HHmm");

	public static SimpleDateFormat SDF_HH_MM = new SimpleDateFormat("HH:mm");

	public static final ThreadLocal<SimpleDateFormat> THREAD_LOCAL = new ThreadLocal<>();

	/**
	 * 获取昨天的初始时间
	 *
	 * @return
	 */
	public static Date getYesterday() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getToday());
		cal.add(Calendar.DATE, -1);

		return cal.getTime();
	}

	/**
	 * 获取给定时间的前一天0点
	 *
	 * @return
	 */
	public static Date getYesterdayTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, -1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取给定时间的当天0点
	 *
	 * @return
	 */
	public static Date getTodayTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}


	/**
	 * 获取给定日期的下一天
	 *
	 * @param startDate
	 * @return
	 */
	public static Date getNextDay(Date startDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDate);
		cal.add(Calendar.DATE, 1);
		return cal.getTime();
	}

	/**
	 * 获取当前时间最近的整15分钟时间
	 *
	 * @return
	 */
	public static Date getLastQuarter(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		//获取当前时间往最近的15分钟时间
		c.set(Calendar.MINUTE,(c.get(Calendar.MINUTE))/15 * 15);
		c.set(Calendar.SECOND,0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}

	/**
	 * 获取给定日期的前或者后几天的日期
	 * 
	 * @param date
	 * @param day
	 * @return
	 * @throws ParseException
	 */
	public static Date getBeforeOrAfterDate(Date date, int day) throws ParseException {
		SimpleDateFormat sdf = THREAD_LOCAL.get();
		if (sdf == null) {
			sdf = new SimpleDateFormat("yyyy-MM-dd");
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, day);

		return sdf.parse(sdf.format(c.getTime()));
	}

	/**
	 * 获取当天0点
	 * 
	 * @return
	 */
	public static Date getToday() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 给定日期，分钟数，根据正负取日期的前几分钟或者后几分钟
	 * 
	 * @param date
	 * @param minute
	 * @return
	 */
	public static Date getBeforeOrAfterMinute(Date date, int minute) {
		Calendar c = Calendar.getInstance();
		c.setTime(date); // 设置时间
		c.add(Calendar.MINUTE, minute);
		date = c.getTime(); // 结果
		return date;
	}

	/**
	 * 获取n分钟后的日期
	 * 
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addMinsDate(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, n);
		return cal.getTime();
	}

	/**
	 * 获取给定日期的所在月第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDateOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取给定日期的上个月的第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDateOfLastMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取给定日期的所在周的第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, 0 * 7);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取给定日期的上一周的第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfLastWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, -1 * 7);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取给定日期的所在年的第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, 0);
		cal.set(Calendar.DAY_OF_YEAR, cal.getActualMinimum(Calendar.DAY_OF_YEAR));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取给定日期的去年的第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfLastYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.YEAR, -1);
		cal.set(Calendar.DAY_OF_YEAR, cal.getActualMinimum(Calendar.DAY_OF_YEAR));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 判断当前时间是否为周一
	 *
	 * @param date
	 * @return
	 */
	public static Boolean isMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		if(null != date) {
			cal.setTime(date);
		}
		int data = cal.get(Calendar.DAY_OF_WEEK);
		return data == Calendar.MONDAY;
	}

	/**
	 * 判断当前时间是否为当月一号
	 *
	 * @param date
	 * @return
	 */
	public static Boolean isFirstDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		if(null != date) {
			cal.setTime(date);
		}
		int data = cal.get(Calendar.DATE);
		return data == 1;
	}

	/**
	 * 判断当前时间是否为当1月1号
	 *
	 * @param date
	 * @return
	 */
	public static Boolean isFirstDayOfYear(Date date) {
		int num=0;
		Calendar cal = Calendar.getInstance();
		if(null != date) {
			cal.setTime(date);
		}
		int day = cal.get(Calendar.DATE);
		int month = cal.get(Calendar.MONTH);
		num =day+ month;
		return num == 2;
	}

	/**
	 * 获取两个日期间的天数
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getDaysByStartAndEnd(Date start, Date end) {
		return (int) ((end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000));
	}

	/**
	 * 获取两个日期间的所有日期(包含开始和结束时间)
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static List<Date> getBetweenDates(Date start, Date end) {
		List<Date> dateList = new ArrayList<Date>();
		while (start.getTime() <= end.getTime()) {
			dateList.add(start);
			start = DateUtil.getNextDay(start);
		}
		return dateList;
	}

	static List<Date> test(Date date) {
		Date start = getZeroTimeOfDate(date);// 转换为天的起始date
		Date nextDayDate = getNextDay(start);// 下一天的date

		List<Date> result = new ArrayList<Date>();
		while (start.compareTo(nextDayDate) < 0) {
			result.add(start);
			// 日期加5分钟
			start = addFiveMin(start, 15);
		}
		return result;
	}

	private static Date addFiveMin(Date start, int offset) {
		Calendar c = Calendar.getInstance();
		c.setTime(start);
		c.add(Calendar.MINUTE, offset);
		return c.getTime();
	}

	public static Date getZeroTimeOfDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date getBeforeOrAfterMonth(Date date, int month) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, month);

		return cal.getTime();
	}

	public static int getBetweenMinute(Date date1, Date date2) {
		// 返回值
		int result = (int) (Math.abs(date1.getTime()-date2.getTime()))-120*60*1000;
		if(result > 0){
			result = 0;
		}else{
			result = 1;
		}
//		return result;
		return 1;
	}
}
